//
//  HAResponse.m
//  HAFund
//
//  Created by 夏成龙 on 2018/10/30.
//  Copyright © 2018年 夏成龙. All rights reserved.
//

#import "HAResponse.h"
#import "FactoryClass.h"

#define mNoDataErrMsg  @"当前未加载到任何数据"
static NSString * const HANetworkErrorDomainResponseCode = @"ECTNetworkErrorDomainResponseCode";


@implementation HAResponse

- (instancetype)initWithResponse:(NSURLResponse *)response responseObject:(id)responseObject error:(NSError *)error {
    if ([super init]) {
        _response = response;
        _error = error;
        
        if (error) {
            NSMutableDictionary *userInfo = [NSMutableDictionary dictionaryWithDictionary:error.userInfo];
            if (error.code == -1009) {
                [userInfo setObject:@"网络异常,连接服务器失败" forKey:@"NSLocalizedDescription"];
            } else if (error.code == -1001) {// 请求超时
                [userInfo setObject:@"网络超时,请检查您的网络" forKey:@"NSLocalizedDescription"];
            }
            NSError *myError = [NSError errorWithDomain:error.domain code:error.code userInfo:userInfo];
            _error = myError;
            return self;
        }
        
        // 解析网关返回数据
        id jsonObject = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:&error];
        
//        NSLog(@"网关返回:%@",jsonObject);
        NSString *data = nil;
        NSData *responseData = nil;
        if ([jsonObject isKindOfClass: NSDictionary.class]) {
            // 解析网关加密内容
            data = jsonObject[@"data"];
            responseData = [[FactoryClass getDecCode:data] dataUsingEncoding:NSUTF8StringEncoding];
        }
      
        // 请求到网关，服务相关错误
        if (data == nil || [data isEqualToString:@""] || responseData.length == 0) {//请求发起成功，处理网关返回是否有错误。
            NSString *responseCode = nil;
            NSString *errorDesc = nil;
            if ([jsonObject isKindOfClass: NSDictionary.class]) {
                // 解析网关加密内容
                responseCode = jsonObject[@"resultCode"];
                errorDesc =  jsonObject[@"errMsg"];
            }
            responseCode = responseCode ? responseCode : [NSNumber numberWithInteger:-999].stringValue;
            errorDesc = errorDesc ? errorDesc : mNoDataErrMsg;
            
            NSError *error = [NSError errorWithDomain:HANetworkErrorDomainResponseCode code:responseCode.integerValue userInfo:@{NSLocalizedFailureReasonErrorKey:responseCode, NSLocalizedDescriptionKey:errorDesc, @"NetworkErrorDetail":jsonObject}];
            _error = error;
            return self;
        }
        
        // 解析服务器返回内容
        NSDictionary *responseJson = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:nil];
        _responseObject = responseJson;
        // 服务器内容报错
        if ([responseJson[@"resultCode"] integerValue] != 1) {
             NSError *error = [NSError errorWithDomain:HANetworkErrorDomainResponseCode code:[responseJson[@"resultCode"] integerValue] userInfo:@{NSLocalizedFailureReasonErrorKey:responseJson[@"resultCode"], NSLocalizedDescriptionKey:responseJson[@"errMsg"], @"NetworkErrorDetail":responseJson}];
            _error = error;
        }
    }
    return self;
}

@end
